﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinnessLogic;
using Model;

namespace scrumbench
{
    public partial class ManageUsersForm : Form
    {
        public ManageUsersForm(string username, int userid, string name, string firstname)
        {
            InitializeComponent();

            this.userid = userid.ToString();
            this.uname = username;
            this.name = name;
            this.firstname = firstname;

        }

        //private int maxteams;

        private string uname;

        
        public string Uname
        {
            get { return uname; }
            set { uname = value; }
        }
        private string userid;

        public string Userid
        {
            get { return userid; }
            set { userid = value; }
        }
        private string name;

        public string Name1
        {
            get { return name; }
            set { name = value; }
        }
        private string firstname;

        public string Firstname
        {
            get { return firstname; }
            set { firstname = value; }
        }

        private void ManageUsersForm_Load(object sender, EventArgs e)
        {
            btnDeleteTeam.Enabled = false;
            button3.Enabled = false;

            TreeNode AllTeams;
            AllTeams = treeView1.Nodes.Add("All");            

            ManageUsers mijnusers = new ManageUsers();

            //roep een nieuwe lijst van allemembers aan
            //de query steekt in de constructor van UserList, dus worden alle users automatisch toegevoegd
            List<User> allmembersZT = new List<User>();
            allmembersZT = mijnusers.PopulateUserlistZonderTeams();
            
            foreach (User mijnuser in allmembersZT)
            {
                
                ListViewItem item = new ListViewItem();
                item.Text = mijnuser.name.ToString();
                item.SubItems.Add(mijnuser.Rolnaam.ToString());
                
                listViewMembers.Items.Add(item);
                item.Tag = mijnuser.UserID;
           
            }

            ManageTeam mijnteams = new ManageTeam();
            PopulateTeamTreeView mijntreeview = new PopulateTeamTreeView();

            List<Team> allteams = new List<Team>();
            allteams = mijntreeview.PopulateTeamlist();        
            
            foreach (Team mijnteam in allteams)
            {    
                TreeNode newNode = new TreeNode();
                newNode.Name = "nd" + mijnteam.ToString();
                newNode.Text = mijnteam.ToString();
                newNode.Tag = mijnteam.teamID;
             // AllTeams.Tag = mijnteam.teamID;
               
              AllTeams.Nodes.Add(newNode); 
            }

          AllTeams.ExpandAll();

        }


        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnNewTeam_Click(object sender, EventArgs e)
        {
            AddTeamForm nAddTeamForm = new AddTeamForm();
            nAddTeamForm.Show();
        }

        private void btnNewMember_Click(object sender, EventArgs e)
        {
            AddUserForm nAddUserForm = new AddUserForm();
            nAddUserForm.Show();
        }

        private void labelName_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void listViewMembers_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            

            
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void listViewMembers_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        private void btnAddMemberToTeam_Click(object sender, EventArgs e)
        {
            AddToTeamForm nAddToTeamForm = new AddToTeamForm();
            nAddToTeamForm.Show();
        }
        private void FilterListView(int filterteamnummer)
        {

            listViewMembers.Items.Clear();

            ManageUsers mijnusers = new ManageUsers();

            //roep een nieuwe lijst van allemembers aan
            //de query steekt in de constructor van UserList, dus worden alle users automatisch toegevoegd
            List<User> allmemberss = new List<User>();
            allmemberss = mijnusers.PopulateUserlist();
            

            foreach (User mijnuser in allmemberss)
            {
                if (filterteamnummer == mijnuser.TeamID)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = mijnuser.name.ToString();
                    item.SubItems.Add(mijnuser.Rolnaam.ToString());
                    listViewMembers.Items.Add(item);
                    item.Tag = mijnuser.UserID;
                }

            }

            listViewMembers.Update();
            
         //   this.Load += new EventHandler(this.listViewMembers_Load);
            // listViewMembers.Focus();

        }

        private void treeView1_NodeMouseDoubleClick_1(object sender, TreeNodeMouseClickEventArgs e)
        {
           if (treeView1.SelectedNode.Text != "All")
            {

                button3.Enabled = true;
                btnDeleteTeam.Enabled = true;

                int filternr;
                filternr = (int)treeView1.SelectedNode.Tag;

                FilterListView(filternr);
            }
            else
            {
                button3.Enabled = false;
                btnDeleteTeam.Enabled = false;

               
                listViewMembers.Items.Clear();

                ManageUsers mijnusers = new ManageUsers();
                List<User> allmembersZT = new List<User>();
                allmembersZT = mijnusers.PopulateUserlistZonderTeams();

                foreach (User mijnuser in allmembersZT)
                {

                    ListViewItem item = new ListViewItem();
                    item.Text = mijnuser.name.ToString();
                    item.SubItems.Add(mijnuser.Rolnaam.ToString());

                    listViewMembers.Items.Add(item);
                    item.Tag = mijnuser.UserID;

                }

                listViewMembers.Update();
            }
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnDeleteMember_Click(object sender, EventArgs e)
        {

            if (listViewMembers.FocusedItem != null)
            {
                    //User tempUser = new User();
                    //tempUser = (User)listViewMembers.;
                    int tempDelMember = (int)listViewMembers.FocusedItem.Tag;
                    DeleteUserORTeam delUoT = new DeleteUserORTeam();
                    delUoT.DeleteUser(tempDelMember);
            }
        }

        private void btnDeleteTeam_Click(object sender, EventArgs e)
        {
            int tempDelTeam = (int)treeView1.SelectedNode.Tag;
            DeleteUserORTeam delUoT = new DeleteUserORTeam();
            delUoT.DeleteTeam(tempDelTeam);

           
            treeView1.Update();
        }

        private void panel8_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void newMemberToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
                AddUserForm tempUserForm = new AddUserForm();
                tempUserForm.Show();

                
            
               
            
        }

        private void membersToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void addMemberToolStripMenuItem_Click(object sender, EventArgs e)
        {
                  AddToTeamForm tempAddToTeamForm = new AddToTeamForm();
                  tempAddToTeamForm.Show();
        }

        private void newTeamToolStripMenuItem_Click(object sender, EventArgs e)
        {
                 AddTeamForm tempTeamForm = new AddTeamForm();
                 tempTeamForm.Show();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

    }
}
